import json
import requests
import numpy as np

header = {
    "Host": "xueqiu.com",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0",
    "Accept": "*/*",
    "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
    "Accept-Encoding": "gzip, deflate, br",
    "Referer": "https://xueqiu.com/hq",
    "cache-control": "no-cache",
    "X-Requested-With": "XMLHttpRequest",
    "Connection": "keep-alive",
    # "Cookie": "Hm_lvt_1db88642e346389874251b5a1eded6e3=1624699349,1624866293,1626663070,1626663099; device_id=f22a9f054403c0c6987cc97218f511d7; s=cp19vaih1o; xq_r_token=c18055138c6787fe5e87aa57072b2abca5f50faf; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjg3MzY0OTk2NzcsImlzcyI6InVjIiwiZXhwIjoxNjI5MjU1MDY5LCJjdG0iOjE2MjY2NjMwNjk1MDgsImNpZCI6ImQ5ZDBuNEFadXAifQ.COSIGOFLqj23CAbvJi5XezU36woiS64ksR05Rqu06xpaUMmt-YbTbQtKh47Panyss6P_5-KMlRn7TD78vDOq27eD6V5kwoqMQJ0of5E_Mcol_xgoE66-Vd7yJd-DBE9mBc911chql4qu-YcG0P8ml6Jj9d4SnH6HP_XMRR5w9Sb_hwMTlPzZLRudYY2QKiO4wbLgOTYgE4AMk42PcyzIfJdaQAWapnT3SneewONB1pV40AJw9oABLkFinkEqRf3IDWTh39AAjSco5bRi-aj1qVu86uuqErQD_q-PGOogHLsy47b744ikbYyZS2IyeeQFfMKFQi1_515Vw24U2MfrxQ; u=8736499677; xq_is_login=1; bid=12f6cbd38037025574fa6c0b4111476f_kqaaqsph; __utma=1.1203836511.1624502332.1624502332.1626663094.2; __utmz=1.1624502332.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); acw_tc=2760820316266630694954278ea6fdbf5733333768138b84c593c55b3612eb; xq_a_token=f16d2cb9c0810372c4dda0a32b8d0da1a097f11a; xqat=f16d2cb9c0810372c4dda0a32b8d0da1a097f11a; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1626664411; snbim_minify=true; __utmb=1.12.10.1626663094; __utmc=1; __utmt=1; acw_sc__v2=60f4ecf424d0ed9ba679bb23b1324264e0d5b43a",
    "Pragma": "no-cache"
}

def radom_sleep():
    import time
    import random
    time.sleep(random.random() * 10 + 10)

def local_time():
    import time
    return time.strftime('%Y-%m-%d', time.localtime(time.time()))

# 获取一页的股票数据
def get_onepage(page_index, f):
    url = "https://xueqiu.com/service/v5/stock/screener/quote/list?page=" + str(
        page_index) + "&size=90&order=desc&orderby=percent&order_by=percent&market=CN&type=sh_sz&_=1626664292055"
    resp = requests.get(url, timeout=30, headers=header)
    res = str(resp.text)
    data = json.loads(res)
    # print(res)
    one_page = data["data"]["list"]
    for one in one_page:
        print(one['symbol'], one['name'], one['current'], one['chg'], one['percent'], one['current_year_percent'],
              one['volume'],
              one['turnover_rate'], one['pe_ttm'], one['pb'], one['followers'], one['market_capital'], one['percent5m'],
              one['followers'])
        output = str(one['symbol']) + ',' + str(one['name']) + ',' + str(one['current']) + ',' + str(one['chg']) + ',' + \
                 str(one['percent']) + ',' + str(one['current_year_percent']) + ',' + str(one['volume']) + ',' + \
                 str(one['turnover_rate']) + ',' + str(one['pe_ttm']) + ',' + str(one['pb']) + ',' + str(one['followers']) + ',' + \
                 str(one['market_capital']) + ',' + str(one['percent5m']) + ',' + str(one['followers']) + '\n'
        f.write(output)


if __name__ == '__main__':
    file_name = 'output-' + local_time() + '.csv'
    f = open(file_name, 'w', encoding='utf-8')
    print('symbol', 'name', 'current', 'chg', 'percent', 'current_year_percent', 'volume',
          'turnover_rate', 'pe_ttm', 'pb', 'followers', 'market_capital', 'percent5m', 'followers')
    f.write('symbol' + ',' + 'name' + ',' + 'current' + ',' + 'chg' + ',' + 'percent' + ',' + 'current_year_percent' + ',' + 'volume' + ',' + \
        'turnover_rate' + ',' + 'pe_ttm' + ',' + 'pb' + ',' + 'followers' + ',' + 'market_capital' + ',' + 'percent5m' + ',' + 'followers' + '\n')
    for i in range(1, 54):
        print('page index', str(i))
        get_onepage(i, f)
        if i % 10 == 0:
            radom_sleep()
    f.close()
